package cn.sczhyt.modules.app.controller;

import cn.sczhyt.common.utils.R;
import cn.sczhyt.modules.app.annotation.Login;
import cn.sczhyt.modules.app.annotation.LoginUser;
import cn.sczhyt.modules.app.entity.UserEntity;
import cn.sczhyt.modules.szhy.entity.MeetingLeaveEntity;
import cn.sczhyt.modules.szhy.service.MeetingLeaveService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.Date;
import java.util.List;


/**
 * 会议请假表
 *
 * @author luojialin
 * @email 78357966@qq.com
 * @date 2021-04-29 16:32:37
 */
@RestController
@RequestMapping("app/meetingleave")
public class AppMeetingLeaveController {
    @Autowired
    private MeetingLeaveService meetingLeaveService;

    /**
     * 请假人列表
     */
    @RequestMapping("/list/{meetingId}")
    public R list(@PathVariable String meetingId){
        QueryWrapper<MeetingLeaveEntity> queryWrapper = new QueryWrapper<MeetingLeaveEntity>()
                .eq("meeting_id",meetingId);

        List<MeetingLeaveEntity> list = meetingLeaveService.list(queryWrapper);

        return R.ok().put("data", list);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{leaveId}")
    public R info(@PathVariable("leaveId") String leaveId){
		MeetingLeaveEntity meetingLeave = meetingLeaveService.getById(leaveId);

        return R.ok().put("meetingLeave", meetingLeave);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MeetingLeaveEntity meetingLeave){
		meetingLeaveService.save(meetingLeave);

        return R.ok();
    }

    /**
     * 添加请假附件
     */
    @ApiOperation("添加请假附件")
    @PostMapping("/saveImages")
    public R save(@RequestParam("file") MultipartFile[] files){


        return meetingLeaveService.saveImages(files);
    }



    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MeetingLeaveEntity meetingLeave){
		meetingLeaveService.updateById(meetingLeave);

        return R.ok();
    }


    /**
     * 同意请假
     */
    @Login
    @RequestMapping("/agree")
    public R agree(@LoginUser UserEntity user,@RequestParam String leaveId){
        MeetingLeaveEntity meetingLeaveEntity = new MeetingLeaveEntity();
        meetingLeaveEntity.setLeaveId(leaveId);
        meetingLeaveEntity.setLeaveStatus("1");
        meetingLeaveEntity.setUpdateUserId(user.getUserId());
        meetingLeaveEntity.setUpdateUsername(user.getUsername());
        System.out.println(meetingLeaveEntity);
        meetingLeaveService.updateById(meetingLeaveEntity);
        return R.ok();
    }


    /**
     * 拒绝请假
     */
    @Login
    @RequestMapping("/refuse")
    public R refuse(@LoginUser UserEntity user, @RequestParam String leaveId, @RequestParam String returnReason){
        MeetingLeaveEntity meetingLeaveEntity = new MeetingLeaveEntity();
        meetingLeaveEntity.setLeaveId(leaveId);
        meetingLeaveEntity.setLeaveStatus("2");
        meetingLeaveEntity.setReturnReason(returnReason);
        meetingLeaveEntity.setUpdateDatetime(new Date());
        meetingLeaveEntity.setUpdateUserId(user.getUserId());
        meetingLeaveEntity.setUpdateUsername(user.getUsername());
        System.out.println(meetingLeaveEntity);
        meetingLeaveService.updateById(meetingLeaveEntity);
        return R.ok();
    }




    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody String[] leaveIds){
		meetingLeaveService.removeByIds(Arrays.asList(leaveIds));

        return R.ok();
    }

}
